<template>
	<view class="order-detail-page">
		<view class="status-tag" v-if="statusText">
			{{ statusText }}
		</view>
		<!-- 信息内容 -->
		<view class="info-section">
			<view class="info-item">
				<text class="label">课程时间</text>
				<text class="value">{{ info.course_camp.class_time_txt || '' }}
					{{ info.course_camp.start_at || '' }}-{{ info.course_camp.end_at || '' }}</text>

			</view>
			<view class="info-item">
				<text class="label">课程名称</text>
				<text class="value">{{ info.pay_body }}</text>
			</view>
			<view class="info-item">
				<text class="label">上课门店</text>
				<text class="value">{{ info.course_camp.shop.shop_name }}</text>
			</view>
			<view class="info-item">
				<view class="label">门店地址</view>
				<view class="value">{{ info.course_camp.shop.address }}</view>
			</view>
			<!-- 
			<view class="info-item">
				<text class="label">实付款</text>
				<text class="value yellow">￥{{ info.pay_amount }}</text>
			</view> -->
		</view>

		<view class="user-section" v-if="userList.list && userList.list.length">
			<view class="section-title">上课用户（{{ userList.user_count }}人）</view>
			<view class="user-item" v-for="(item, index) in userList.list" :key="index">
				<view class="user-info">
					<view class="nickname">{{ item.user.nickname || '未填写昵称' }}</view>
					<view class="mobile">手机号：{{ item.user.mobile || '保密' }}</view>

					<view class="stats">

						<span>下单时间：{{ item.pay_time || 0 }}</span>
						<span class="yellow">￥{{ info.pay_amount }}</span>
					</view>
				</view>
			</view>
		</view>


		<view v-if="is_type" class="detail-popup">
			<template v-if="info.order_status == 1">
				<view class="detail-title">课程：{{ detailData.title }}</view>
				<view class="upload-box">
					<block v-for="(img, idx) in detailData.images" :key="idx">
						<view class="img-wrap">
							<image :src="web_url + img" class="img"></image>
							<view class="del-btn" @click.stop="removeImg(idx)">×</view>
						</view>
					</block>
					<view v-if="detailData.images.length < 5" class="img-wrap add" @click="chooseImage">+</view>
				</view>
				<button class="confirm-btn" @click="confirmClass"> {{ confirmClassText }}</button>
			</template>
			<template v-else>
					<button class="confirm-btn">已确认上课</button>
			</template>
		</view>
		
	</view>
</template>

<script>
	import uploads from '@/util/upload.js'
	const webConfig = require("@/util/config");
	export default {
		data() {
			return {
				info: {},
				userList: {
					list: [],
					user_count: 0
				},
				detailData: {
					title: '',
					images: []
				},
				statusText: '',
				web_url: "",
				is_type: '',
				option_id: "",
				goods_id: '',
				confirmClassText: '确认上课'
			};
		},
		onLoad(e) {
			this.is_type = e.is_type || ''
			this.option_id = e.id
			this.web_url = webConfig.base_url || '';
			this.goods_id = e.goods_id || ''
			if (e.is_type) {
				this.getWaitDetail(e.id, e.is_type)
			} else {
				this.getOrderDetail()
			}

		},
		methods: {
			getOrderDetail() {
				let _this = this
				this.$http({
					url: '/addons/xilufitness/coach/getOrderDetail',
					method: 'get',
					data: {
						id: _this.option_id,
						goods_id: _this.goods_id
					}
				}).then(res => {
					if(res.code == 1) {
						_this.info = res.data.info;
						_this.userList = res.data.userList || {
							list: [],
							user_count: 0
						};
						const statusMap = {
							0: '未支付',
							1: '待上课',
							2: '进行中',
							3: '已完成',
							4: '已取消',
							5: '已过期',
							6: '已评论',
							10: '在排队',
							11: '已退费'
						};
						_this.statusText = statusMap[_this.info.order_status] || '';
					} else {
						this.$api.toast(res.msg);
					}
				
				}).catch(err => {
					console.error('订单详情错误', err);
				});
			},
			getWaitDetail(id, is_type) {
				let _this = this
				this.$http({
					url: '/addons/xilufitness/coach/getWaitDetail',
					method: 'get',
					data: {
						id: id,
						is_type: is_type
					}
				}).then(res => {
					_this.info = res.data.info;
					_this.userList = res.data.userList || {
						list: [],
						user_count: 0
					};
					if(res.data.info.order_status == 1) {
						_this.confirmClassText = '确认上课'
						
					} else {
						_this.confirmClassText = '已确认上课'
					}
					_this.statusText = '待上课课程';
				}).catch(err => {
					console.error('订单详情错误', err);
				});
			},
			confirmClass() {
				let _this = this
				this.$http({
					url: "/addons/xilufitness/coach/getSureCourse",
					data: {
						is_type: _this.is_type,
						id: _this.option_id,
						image: _this.detailData.images || []
					}
				}).then(res => {
						_this.$api.toast(res.msg);
					if(res.code == 1) {
						
						_this.confirmClassText = res.msg
					}
					
					console.log(res);
				})
			},
			chooseImage() {
				let _this = this;
				uni.chooseImage({
					count: 5 - _this.detailData.images.length,
					success(res) {
						let tempPath = res.tempFilePaths[0];
						uni.showLoading({
							title: '上传中'
						});
						uploads.uploadFile(tempPath, function(uploadRes) {
							uni.hideLoading();
							if (uploadRes.code == 1) {
								_this.detailData.images.push(uploadRes.data.url);
							} else {
								uni.showToast({
									title: uploadRes.msg || '上传失败',
									icon: 'none'
								});
							}
						});
					}
				});
			},
			removeImg(idx) {
				this.detailData.images.splice(idx, 1);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.status-tag {
		background-color: #FFCF00;
		color: #181818;
		padding: 10rpx 20rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		display: inline-block;
		margin-bottom: 20rpx;
		font-weight: bold;
	}

	.detail-popup {
		margin-top: 30rpx;
		padding: 30rpx;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.4);

		.detail-title {
			font-size: 30rpx;
			color: #fff;
			margin-bottom: 20rpx;
		}

		.upload-box {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			margin-bottom: 30rpx;

			.img-wrap {
				position: relative;
				width: 160rpx;
				height: 160rpx;
				border-radius: 12rpx;
				overflow: hidden;
				background-color: #fff;

				.img {
					width: 100%;
					height: 100%;
					object-fit: cover;
					border-radius: 12rpx;
				}

				.del-btn {
					position: absolute;
					top: 0;
					right: 0;
					width: 40rpx;
					height: 40rpx;
					line-height: 40rpx;
					text-align: center;
					background: rgba(0, 0, 0, 0.5);
					color: #fff;
					border-bottom-left-radius: 12rpx;
					font-size: 30rpx;
				}

				&.add {
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 50rpx;
					color: #666;
					background-color: #E8EEFE;
				}
			}
		}

		.confirm-btn {
			width: 100%;
			padding: 20rpx 0;
			text-align: center;
			border-radius: 12rpx;
			background-color: #FFCF00;
			color: #181818;
			font-size: 32rpx;
			transition: 0.2s;

			&:active {
				opacity: 0.8;
				transform: scale(0.96);
			}
		}
	}

	.order-detail-page {

		background-color: #E8EEFE;
		color: #333;
		padding: 20rpx;
		min-height: 100vh;
	}

	.header-card {
		display: flex;
		align-items: center;
		background-color: #111;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.logo {
		width: 120rpx;
		height: 120rpx;
		border-radius: 20rpx;
		background-color: #222;
		margin-right: 30rpx;
	}

	.course-info {
		flex: 1;
	}

	.course-title {
		font-size: 34rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.price {
		font-size: 28rpx;
	}

	.price-highlight {
		color: #ffcf00;
		font-size: 40rpx;
		font-weight: bold;
	}

	.info-section {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.info-item {
		margin-bottom: 30rpx;
	}

	.label {
		font-size: 26rpx;
		color: #aaa;
		margin-bottom: 6rpx;
		display: block;
	}

	.value {
		font-size: 30rpx;
		color: #333;
		word-break: break-word;
	}

	.yellow {
		color: #ffcf00;
		font-weight: bold;
	}

	.user-section {
		margin-top: 40rpx;
	}

	.section-title {
		font-size: 30rpx;
		margin-bottom: 20rpx;
		font-weight: bold;
	}

	.user-item {
		padding: 20rpx;
		border-bottom: 1rpx solid #333;
	}

	.nickname {
		font-size: 30rpx;
		color: #333;
	}

	.mobile {
		font-size: 24rpx;
		color: #ccc;
		margin: 10rpx 0;
	}

	.stats {
		font-size: 24rpx;
		color: #999;
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}
</style>